package com.ifengchao.dao.impl;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ifengchao.dao.IBatisBaseDao1;
import com.ifengchao.dao.iface.DealZzyPlanDao;
import com.ifengchao.pojo.ZzyPlanInfo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * Created by HuangYL on 2017/8/29.
 */
@Repository
public class DealZzyPlanDaoImpl implements DealZzyPlanDao {
    private static final Logger logger = Logger.getLogger(DealZzyPlanDaoImpl.class);

    @Autowired(required = true)
    private IBatisBaseDao1 iBatisBaseDao1;


    @Override
    public List<ZzyPlanInfo> getZzyPlanList() {
        logger.info("查询所有定时的广告计划");
        List<ZzyPlanInfo> planList = iBatisBaseDao1.getSqlMapClientTemplate().queryForList("dealZzyPlanSql.getZzyPlanList");
        return planList;
    }

    @Override
    public void updateShelfZzyPlan(List<Map<String, Object>> list, final int bar) {
        logger.info("更新智子云计划");
        final List<Map<String, Object>> tempList = list;
        try {
            if (list != null) {
                iBatisBaseDao1.getSqlMapClientTemplate().execute(
                        new SqlMapClientCallback() {
                            public Object doInSqlMapClient(SqlMapExecutor executor) throws
                                    SQLException {
                                executor.startBatch();
                                int batch = 0;
                                for (Map<String, Object> obj : tempList) {
                                    executor.update("dealZzyPlanSql.updateShelfZzyPlan", obj);
                                    batch++;
                                    if (batch == bar) {
                                        executor.executeBatch();
                                        batch = 0;
                                    }
                                }
                                executor.executeBatch();
                                return null;
                            }
                        }
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updateShelfZzyAdvertise(List<Map<String, Object>> list, final int bar) {
        logger.info("更新智子云计划下所有广告");
        final List<Map<String, Object>> tempList = list;
        try {
            if (list != null) {
                iBatisBaseDao1.getSqlMapClientTemplate().execute(
                        new SqlMapClientCallback() {
                            public Object doInSqlMapClient(SqlMapExecutor executor) throws
                                    SQLException {
                                executor.startBatch();
                                int batch = 0;
                                for (Map<String, Object> obj : tempList) {
                                    executor.update("dealZzyPlanSql.updateShelfZzyAdvertise", obj);
                                    batch++;
                                    if (batch == bar) {
                                        executor.executeBatch();
                                        batch = 0;
                                    }
                                }
                                executor.executeBatch();
                                return null;
                            }
                        }
                );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
